package com.bc.ljn.catwars;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

/**
 * Created by ljn on 2014/12/24.
 */
public class Bullet {
    //子弹图片资源
    public Bitmap bmpBullet;
    //子弹的坐标
    public int bulletX, bulletY;
    //子弹的速度
    public int speed;
    //子弹的种类以及常量
    public int bulletType;
    //主角的
    public static final int BULLET_PLAYER = -1;
    //dog的
    public static final int BULLET_DOG = 1;
    //boss的
    public static final int BULLET_BOSS = 3;
    //子弹是否超屏，优化处理
    public boolean isDead;

    //Boss 疯狂状态下子弹相关成员变量
    private int dir;//当前Boss子弹方向
    //8 方向常量
    public static final int DIR_UP = -1;
    public static final int DIR_DOWN = 2;
    public static final int DIR_LEFT = 3;
    public static final int DIR_RIGHT = 4;
    public static final int DIR_UP_LEFT = 5;
    public static final int DIR_UP_RIGHT = 6;
    public static final int DIR_DOWN_LEFT = 7;
    public static final int DIR_DOWN_RIGHT = 8;


    //子弹构造函数
    public Bullet(Bitmap bmpBullet, int bulletX, int bulletY, int bulletType) {
        this.bmpBullet = bmpBullet;
        this.bulletX = bulletX;
        this.bulletY = bulletY;
        this.bulletType = bulletType;
        //不同的子弹类型速度不一
        switch (bulletType) {
            case BULLET_PLAYER:
                speed = 20;
                break;
            case BULLET_DOG:
                speed = 8;
                break;
            case BULLET_BOSS:
                speed = 5;
                break;
        }
    }

    /**
     * 专用于处理Boss疯狂状态下创建的子弹
     *
     * @param bmpBullet
     * @param bulletX
     * @param bulletY
     * @param bulletType
     * @param dir
     */
    public Bullet(Bitmap bmpBullet, int bulletX, int bulletY, int bulletType, int dir) {
        this.bmpBullet = bmpBullet;
        this.bulletX = bulletX;
        this.bulletY = bulletY;
        this.bulletType = bulletType;
        speed = 5;
        this.dir = dir;
    }

    //子弹的绘制
    public void draw(Canvas canvas, Paint paint) {
        canvas.drawBitmap(bmpBullet, bulletX, bulletY, paint);
    }

    //子弹的逻辑
    public void logic() {
        //不同的子弹类型逻辑不一
        //主角的子弹垂直向上运动
        switch (bulletType) {
            case BULLET_PLAYER:
                bulletY -= speed;
                if (bulletY < -50) {
                    isDead = true;
                }
                break;
            //dog和boss的都是垂直下落运动
            case BULLET_DOG:
                bulletY += speed;
                if (bulletY > GameView.screenH) {
                    isDead = true;
                }
                break;
            case BULLET_BOSS:
                bulletY += speed;
                if (bulletY > GameView.screenH) {
                    isDead = true;
                }
                //boss 疯狂状态下的逻辑待实现
                switch (dir) {
                    case DIR_UP:
                        bulletY -= speed;
                        break;
                    case DIR_DOWN:
                        bulletY += speed;
                        break;
                    case DIR_LEFT:
                        bulletX -= speed;
                        break;
                    case DIR_RIGHT:
                        bulletX += speed;
                        break;
                    case DIR_UP_LEFT:
                        bulletX -= speed;
                        bulletY -= speed;
                        break;
                    case DIR_UP_RIGHT:
                        bulletX += speed;
                        bulletY -= speed;
                        break;
                    case DIR_DOWN_LEFT:
                        bulletX -= speed;
                        bulletY += speed;
                        break;
                    case DIR_DOWN_RIGHT:
                        bulletX += speed;
                        bulletY += speed;
                        break;
                }
                //边界处理
                if (bulletY > GameView.screenH || bulletY <= -40 ||
                        bulletX > GameView.screenW || bulletX <= -40) {
                    isDead = true;
                }
                break;
        }
    }
}
